<?php   
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('functions.php'); //Require/include the functions file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['stoptrackingfile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Stop Tracking a file'));
    } else {
        //Connect to DB and connect to the database using db.php
        $db = new my_db;
        //See if a value has been given for the deleteid if not display the form
        if (isset($_POST['deleteid'])) {
            if (empty($_POST['deleteid'])) {
                die ("<BR><div align='center'><font size='4' face='Arial Black'> Error: </font><font size='4' face='Arial' color='red'> An unknown error has occurred!</font><BR><a href=\"javascript:history.back(1)\">Back</a>");
            }
             
            $deleteid = mysql_escape_string($_POST['deleteid']);
             
            template_headtag('Stop Tracking');
            template_header();
            template_left();
            echo "<div class='pageheadertext'>Stop Tracking</div>";
             
            $db->query("SELECT * FROM trackedfiles WHERE id='$deleteid'");
            $db->next_record();
            if ($db->nf() > 0) {
                $filename = strip_tags($db->f('filename'));
                $filepath = strip_tags($db->f('filepath'));
                $lastrevdate = date("Y-d-m h:i:s");
                $lastuser = $_SESSION['username'];
                 
                $db->query("DELETE FROM trackedfiles WHERE id='$deleteid'");
                $db->query("INSERT INTO history (filename, filepath, revhash, revdate, user, revsummary) VALUES('$filename','$filepath','00000000000000000000000000000000','$lastrevdate', '$lastuser','Tracking stopped') ");
                echo "<div align='center'>Tracking stopped<br/><a href='javascript:history.back(2)'>Back</a></div>";
            } else {
                echo "<br/>";
                errorbox('File not found!');
                echo "<br/><div align='center'><a href='javascript: history.go(-1)'>Back</a></div>";
                 
            }
             
             
             
            template_footer();
             
        } else {
            $id = strip_tags(mysql_escape_string($_GET['id']));
            template_headtag('Stop Tracking');
            template_header();
            template_left();
            $db->query("SELECT * FROM trackedfiles WHERE id='$id'");
            $db->next_record();
            if ($db->nf() > 0) {
                $filename = strip_tags($db->f('filename'));
                $filepath = strip_tags($db->f('filepath'));
                echo "<div class='pageheadertext'>Stop Tracking $filename</div>";
                echo "
                    <form method='post' action='stoptrackingfile.php'>
                    <div align = 'center'>
                    <p>Are you sure you want to stop tracking $filepath$filename ?<br/></p>
                    <input type='hidden' name='deleteid' value='$id'/>
                    <input type='submit' value='Yes'/>
                    </div></form>
                     
                    ";
                echo "<br/><div align='center'><a href='javascript: history.go(-1)'>Back</a></div>";
                 
            } else {
                errorbox('File not found!');
            }
            template_footer();
             
        }
    }
?>
